﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace PSE.Deployment.Setup.Uninstall
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            try
            {
                string sourceFolder = System.Windows.Forms.Application.StartupPath;
                string destinationFolder = Path.Combine(Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.Machine), "PSESetup");

                if (!Directory.Exists(destinationFolder))
                    Directory.CreateDirectory(destinationFolder);

                foreach (string file in Directory.GetFiles(sourceFolder))
                {
                    File.Copy(file, Path.Combine(destinationFolder, Path.GetFileName(file)), true);
                }

                RunSetupProcess(Path.Combine(destinationFolder, "PSESetup.exe"));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);

                System.Diagnostics.EventLog.WriteEntry("PSE Setup", ex.ToString(), EventLogEntryType.Error);
            }
        }

        public static void RunSetupProcess(string fileName)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = fileName;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.UseShellExecute = false;

            proc.Start();
        }
    }
}
